package com.jblv.frame.common.utils;

import com.jblv.frame.common.enums.ExceptionEnum;
import com.jblv.frame.common.message.Result;
import com.jblv.frame.common.type.BooleanType;
import com.jblv.frame.common.enums.ResultCodeEnum;

/**
 * <p>响应返回数据封装</p>
 *
 * @author onion.yangtao
 * @version 1.0
 * @date 2019/6/15 0015
 * @since jdk1.8
 */
public class ResultUtils {


    public static Result error() {
        return error(ResultCodeEnum.FAILD_OPERATE_CODE);
    }

    public static Result error(String message) {
        return error(ResultCodeEnum.FAILD_OPERATE_CODE.getCode(),message);
    }

    public static Result error(ResultCodeEnum resultCodeEnum) {
        return error(resultCodeEnum.getCode(),resultCodeEnum.getMessage());
    }

    public static Result error(ExceptionEnum exceptionEnum) {
        return error(exceptionEnum.getCode(),exceptionEnum.getMessage());
    }

    public static Result success(ResultCodeEnum resultCodeEnum) {
        return error(resultCodeEnum.getCode(),resultCodeEnum.getMessage());
    }


    /**
     * 发生错误返回
     *
     * @param code
     * @param message
     * @return
     */
    public static Result error(int code, String message) {
        Result result = new Result();
        result.setCode(code);
        result.setStatus(BooleanType.FALSE.getCode());
        result.setMessage(message);
        return result;
    }

    /**
     * 自定义异常返回
     * @param exceptionEnum
     * @return
     */
    public static Result exception(ExceptionEnum exceptionEnum) {
        Result result = new Result();
        result.setCode(exceptionEnum.getCode());
        result.setStatus(BooleanType.FALSE.getCode());
        result.setMessage(exceptionEnum.getMessage());
        return result;
    }

    /**
     * 业务成功返回
     * @param
     * @return
     */
    public static Result success() {
        return success(null, ResultCodeEnum.SUCCESS_CODE.getMessage(),ResultCodeEnum.SUCCESS_CODE.getCode(),BooleanType.TRUE.getCode());
    }

    /**
     * 业务成功返回
     * @param data
     * @return
     */
    public static Result success(Object data) {
        return success(data, ResultCodeEnum.SUCCESS_CODE.getMessage());
    }

    public static Result success(Object data, String message) {
        Result result = success(data,message,ResultCodeEnum.SUCCESS_CODE.getCode(),BooleanType.TRUE.getCode());
        return result;
    }

    private static Result success(Object data, String message, int code,int state) {
        Result result = new Result();
        result.setCode(code);
        result.setStatus(state);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    private static Result success(Object data,ResultCodeEnum resultCodeEnum, BooleanType booleanType) {
        Result result = success(data, resultCodeEnum.getMessage(),resultCodeEnum.getCode(),booleanType.getCode());
        return result;
    }
}
